package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jaya.budan.R;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.views.ExpandableTextView;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.Utils;
import com.jaya.budan.views.Circle_ImageView;

import java.util.ArrayList;

public class NearBy_ListView_Adapter extends BaseAdapter {

    private Context mcontext;
    private ArrayList<User_Limit> mlist;
    private int mtype;
    public NearBy_ListView_Adapter(Context context, ArrayList<User_Limit> list, int type){
        mcontext = context;
        mlist = list;
        mtype = type;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mtype == 1){
            convertView = View.inflate(mcontext, R.layout.nearby_listview_item, null);
            Circle_ImageView iv_nearby_item_icon = convertView.findViewById(R.id.iv_nearby_item_icon);
            TextView tv_nearby_listview_item_online = convertView.findViewById(R.id.tv_nearby_listview_item_online);
            ExpandableTextView tv_nearby_listview_item_gxqm = convertView.findViewById(R.id.tv_nearby_listview_item_gxqm);
            TextView tv_nearby_listview_item_height = convertView.findViewById(R.id.tv_nearby_listview_item_height);
            Glide.with(mcontext).load(mlist.get(position).getHead_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(iv_nearby_item_icon);
            if (mlist.get(position).getIs_online() == 1){ //离线s
                tv_nearby_listview_item_online.setText("离线");
            } else {
                tv_nearby_listview_item_online.setText("在线");
            }
            int viewWidth = ToolUtils.getWindowWidth((Activity) mcontext) - Utils.dp2px(mcontext, 20f);
            tv_nearby_listview_item_gxqm.setText("" + mlist.get(position).getRemark());
            tv_nearby_listview_item_height.setText("" + mlist.get(position).getAge() + "/" + mlist.get(position).getHeight() + "/" + mlist.get(position).getWeigh()+ "/" + mlist.get(position).getRole());
        } else {
            convertView = View.inflate(mcontext, R.layout.nearby_gridview_item, null);
            LinearLayout rl_gv_item_bg = convertView.findViewById(R.id.rl_gv_item_bg);
            ImageView ll_nearby_gridview_item = convertView.findViewById(R.id.ll_nearby_gridview_item);
            ImageView iv_chat_item_online = convertView.findViewById(R.id.iv_chat_item_online);
            SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                    rl_gv_item_bg.setBackground(resource);
                }
            };
            Glide.with(mcontext).load(mlist.get(position).getHead_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(simpleTarget);
            Glide.with(mcontext).load(mlist.get(position).getHead_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(ll_nearby_gridview_item);
            if (mlist.get(position).getIs_online() == 1){ //离线
                iv_chat_item_online.setVisibility(View.GONE);
            } else {
                iv_chat_item_online.setVisibility(View.VISIBLE);
            }
        }
        TextView tv_nearby_listview_item_juli = convertView.findViewById(R.id.tv_nearby_listview_item_juli);
        TextView tv_nearby_listview_item_name = convertView.findViewById(R.id.tv_nearby_listview_item_name);
        ImageView iv_nearby_item_isvip = convertView.findViewById(R.id.iv_nearby_item_isvip);
        ImageView iv_nearby_item_sex = convertView.findViewById(R.id.iv_nearby_item_sex);
        tv_nearby_listview_item_name.setText("" + mlist.get(position).getNicename());
        if (mlist.get(position).getSex().equals("1")){ //男
            iv_nearby_item_sex.setImageResource(R.mipmap.boy);
        } else {
            iv_nearby_item_sex.setImageResource(R.mipmap.girl);
        }
        tv_nearby_listview_item_juli.setText(mlist.get(position).getDistance());
        if (mlist.get(position).getIs_vip() == 1){
            tv_nearby_listview_item_name.setTextColor(Color.parseColor("#FF3B30"));
            iv_nearby_item_isvip.setVisibility(View.VISIBLE);//是 会员
        } else {
            tv_nearby_listview_item_name.setTextColor(Color.parseColor("#000000"));
            iv_nearby_item_isvip.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
}
